<?php

namespace app\web\controller;
use app\web\model\ChainStoreModel;
use app\web\model\OrderprtModel;
use app\admin\model\Member as MemberModel;
use app\common\controller\StoreBaseController;
use think\Db;
/**
 * 连锁店
 */
class ChainStoreController extends StoreBaseController
{
 	 protected $model;
	 protected $orderprtmodel;
	 protected $membermodel;
	
    protected function _initialize() {
        parent::_initialize();
        $this->model = new ChainStoreModel();
		$this->orderprtmodel = new OrderprtModel();
		$this->membermodel = new MemberModel();
		$count=Db::name('mall_chainstore')->where('agent_id',session('storeInfo')['id'])->count();
 		 $this->assign('count',$count);
    }
    /**
     * 连锁店列表
     * @author [longx] 
     */
    public function index( $page = 1){
        $list = $this->model->paginate(10);
		$field = 'm.*,g.id as sid';
		$list=Db::name("mall_chainstore")->alias('m')
		->join('mall_orderprt g on','g.store_id = m.id','LEFT')
		->join('long_member h on','h.id = m.u_id','LEFT')
		->field($field)->where('m.agent_id',session('storeInfo')['id'])->paginate(10);
        return $this->fetch('index', ['list' => $list]);
    }


    /**
     * 
     *添加
     * @author [longx] 
     */
    public function add()
    {  	
        if(request()->isPost()){
			//获取session中商家id
			
			$param['agent_id'] = session('storeInfo')['id'];
			$param['mobile']   = input('store_tel');
			$param['name']   = input('name');
			$param['shopowner']   = input('shopowner');
			$param['longitude']   = input('longitude');
			$param['latitude']   = input('latitude');
			$param['address']   = input('address');
			
			if ($this->model->allowField(true)->save($param) !== false) {
	                $this->success('添加成功', url('chain_store/index'));
	        } else {
	            $this->error('添加失败');
	        }			
        }
        return $this->fetch();
    }


    /**
     * [edit 编辑]
     * @return [type] [description]
     * @author [longx]
     */
    public function edit($id)
    {	
        if(request()->isPost()){
            $param = input('post.');  
			//会员信息$where
			$param['agent_id'] = session('storeInfo')['id'];
			$param['mobile']   = input('store_tel');
			$param['name']   = input('name');
			$param['shopowner']   = input('shopowner');
			$param['longitude']   = input('longitude');
			$param['latitude']   = input('latitude');
			$param['address']   = input('address');
			
	         	if ($this->model->allowField(true)->save($param,$id) !== false) {
	                    $this->success('修改成功', url('chain_store/index'));
	            } else {
	                $this->error('修改失败');
	            }
			
        }
        $id = input('param.id');
        $this->assign('ad',$this->model->find($id));
        return $this->fetch();
    }
	
	
	 /**
     * [edit 打印机设置]
     * @return [type] [description]
     * @author [longx]
     */
    public function prtadd($id)
    {
        if(request()->isAjax()){
        	$param = input('post.');
			if ($this->orderprtmodel->allowField(true)->save($param) !== false) {
                    $this->success('添加成功', url('chain_store/index'));
            } else {
                $this->error('添加失败');
            }
        }
		$this->assign('store_id',$id);
        $this->assign('ad',$this->orderprtmodel->where('store_id',$id)->find());
        return $this->fetch();
    }
	
	/**
     * [edit 打印机设置]
     * @return [type] [description]
     * @author [longx]
     */
    public function prtedit($id)
    {
    	$orderprt = db('mall_orderprt')->where('store_id',$id)->find();
      	if(request()->isAjax()){
            $param = input('post.');    
         	if ($this->orderprtmodel->allowField(true)->save($param,['id' => $orderprt['id']]) !== false) {
                    $this->success('修改成功', url('chain_store/index'));
            } else {
                $this->error('修改失败');
            }
        }
        $this->assign('ad',$this->orderprtmodel->where('id',$orderprt['id'])->find());
        return $this->fetch();
    }
	



    /**
     * [del删除]
     * @author [longx] 
     */
    public function delete()
    {
        $id = input('param.id');
        if ($this->model->destroy($id)) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }
}
